package com.xlh.thread;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


import com.xlh.Utils.HttpHelper;
import com.xlh.conf.CommonConf;
import com.xlh.interf.IUiUpdateHandler;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TestTask implements Runnable{



	public Context context = null;

	private Handler handler;
	private String type;





	public String getType() {
		return type;
	}

	public TestTask setType(String type) {
		this.type = type;
		return this;
	}

	public Context getContext() {
		return context;
	}




	public TestTask setContext(Context context) {

		this.context = context;
		return this;
	}

	public Handler getHandler() {
		return handler;
	}

	public TestTask setHandler(Handler handler) {
		this.handler = handler;
		return this;
	}

	private String url;

	public String getUrl() {
		return url;
	}

	public TestTask setUrl(String url) {
		this.url = url;
		return this;
	}

	@Override
	public void run() {

		//String url = CommonConf.checkVerUrl;
		// TODO Auto-generated method stub
		OkHttpClient okHttpClient =new OkHttpClient.Builder()
				.connectTimeout(50, TimeUnit.SECONDS)
				.readTimeout(50, TimeUnit.SECONDS)
				.writeTimeout(30, TimeUnit.SECONDS)

				.build();



		Request request = new Request.Builder().url(url).addHeader("Connection","close").build();
		Call call = okHttpClient.newCall(request);
		try {
			Response response = call.execute();
			//Log.e("OKHTTP",response.body().string());
			Message m = new Message();
			Bundle b = new Bundle();

			b.putCharSequence("result",response.body().string() );
			b.putString("type",getType());
			b.putString("err","false");
			m.setData(b);

			//是否传入handler
//			if(handler!=null){
//				handler.sendMessage(m);
//			}else{
				if( ((IUiUpdateHandler)context).getHandler()!=null ){
					((IUiUpdateHandler)context).getHandler().sendMessage(m);
				}
//			}




		} catch (IOException e) {
			e.printStackTrace();
			Message m = new Message();
			Bundle b = new Bundle();

			b.putCharSequence("result","" );
			b.putString("type",getType());
			b.putString("err","true");
			m.setData(b);
			if( ((IUiUpdateHandler)context).getHandler()!=null ){
				((IUiUpdateHandler)context).getHandler().sendMessage(m);
			}



		}


	}
}
